package javolution.context;

import javolution.xml.XMLSerializable;

/* loaded from: classes2.dex */
public abstract class Context implements XMLSerializable {
    private AllocatorContext _allocator;
    private ObjectFactory _factory;
    private Context _outer;
    private Thread _owner;
    public static final Context ROOT = new Root();
    private static final ThreadLocal CURRENT = new ThreadLocal() { // from class: javolution.context.Context.1
        @Override // java.lang.ThreadLocal
        protected Object initialValue() {
            return Context.ROOT;
        }
    };

    /* loaded from: classes2.dex */
    private static final class Root extends Context {
        private Root() {
        }

        @Override // javolution.context.Context
        protected void enterAction() {
            throw new UnsupportedOperationException("Cannot enter the root context");
        }

        @Override // javolution.context.Context
        protected void exitAction() {
            throw new UnsupportedOperationException("Cannot enter the root context");
        }
    }

    public static final <T extends Context> T enter(Class<T> cls) {
        ObjectFactory objectFactory = ObjectFactory.getInstance(cls);
        T t = (T) objectFactory.object();
        ((Context) t)._factory = objectFactory;
        enter(t);
        return t;
    }

    public static final <T extends Context> T enter(T t) {
        if (((Context) t)._owner == null) {
            Context current = getCurrent();
            ((Context) t)._outer = current;
            ((Context) t)._owner = Thread.currentThread();
            ((Context) t)._allocator = t instanceof AllocatorContext ? (AllocatorContext) t : current._allocator;
            CURRENT.set(t);
            t.enterAction();
            return t;
        }
        throw new IllegalStateException("Context is currently in use");
    }

    /* JADX WARN: Type inference failed for: r2v1, types: [javolution.context.AllocatorContext, java.lang.Thread, javolution.context.ObjectFactory, javolution.context.Context] */
    public static Context exit() {
        Context current = getCurrent();
        Context context = current._outer;
        if (context == null) {
            throw new IllegalStateException(Thread.currentThread() + " Cannot exit instance of " + current.getClass());
        }
        try {
            current.exitAction();
            return current;
        } finally {
            CURRENT.set(context);
            current._outer = null;
            current._owner = null;
            current._allocator = null;
            ObjectFactory objectFactory = current._factory;
            if (objectFactory != null) {
                objectFactory.recycle(current);
                current._factory = null;
            }
        }
    }

    public static final void exit(Context context) {
        exit();
    }

    public static Context getCurrent() {
        return (Context) CURRENT.get();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public static void setCurrent(ConcurrentContext concurrentContext) {
        CURRENT.set(concurrentContext);
    }

    protected abstract void enterAction();

    protected abstract void exitAction();

    /* JADX INFO: Access modifiers changed from: package-private */
    public final AllocatorContext getAllocatorContext() {
        AllocatorContext allocatorContext = this._allocator;
        return allocatorContext == null ? AllocatorContext.getDefault() : allocatorContext;
    }

    public final Context getOuter() {
        return this._outer;
    }

    public final Thread getOwner() {
        return this._owner;
    }

    public String toString() {
        return "Instance of " + getClass().getName();
    }
}
